﻿using Biz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App
{
    public partial class FrmDonHang : Form
    {
        public FrmDonHang()
        {
            InitializeComponent();
        }
        List<Order> orders;
        List<ReceiptNote> receipts;
        ReceiptNote receipt;
        Order order;
        //OrderProduct 
        private void FrmDonHang_Load(object sender, EventArgs e)
        {
            order = new Order();
            orders = order.GetAll();
            receipt = new ReceiptNote();
            receipts = receipt.GetAll();
            loadDsHoaDon();
            loadDsPhieuNhap();
            loadCbxStatus();
        }

        #region order
        public void loadDsHoaDon()
        {
            lstDsHoaDon.Items.Clear();
            foreach (var o in orders)
            {
                ListViewItem lvi = new ListViewItem(o.Id.ToString());
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, String.Format("{0:d/M/yyyy}", o.Time));
                lvi.SubItems.Add(sit);
                lvi.Tag = o;
                lstDsHoaDon.Items.Add(lvi);

            }
        }

        public void DisplayOrderDetail()
        {
            txtName.Text = order.CustomerName;
            txtEmail.Text = order.CustomerEmail;
            txtPhone.Text = order.CustomerPhone;
            txtAddress.Text = order.CustomerAddress;
            lstDsChiTiet.Items.Clear();
            chkStatus.Checked = (int)order.Status == 0 ? true : false;
            chkStatus.Enabled = (int)order.Status == 0 ? false : true;
            //cktStatus.Checked = order.Status.GetTypeCode;
            //txtAddress.Text = order.Status.ToString();
            int Total = 0;
            foreach (var o in order.Products)
            {
                int subTotal = o.Quantity * o.Price;
                ListViewItem lvi = new ListViewItem(o.Product.ToString());
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, o.Price.ToString());
                ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem(lvi, o.Quantity.ToString());
                ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem(lvi, subTotal.ToString());
                lvi.SubItems.Add(sit);
                lvi.SubItems.Add(sit1);
                lvi.SubItems.Add(sit2);
                lvi.Tag = o;
                lstDsChiTiet.Items.Add(lvi);
                Total += subTotal;
            }
            txtTotal.Text = Total.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int status = int.Parse(cbxStatus.SelectedValue.ToString());
            DateTime from = DateTime.Parse(string.Format("{0} 00:00:00", String.Format("{0:MM/dd/yyyy}", dtpNgayDau.Value)));
            DateTime to = DateTime.Parse(string.Format("{0} 23:59:59", String.Format("{0:MM/dd/yyyy}", dtpNgayCuoi.Value)));
            order = new Order();
            orders = order.GetByCondition(from, to, status);
            loadDsHoaDon();
        }

        private void btnXemDonHang_Click(object sender, EventArgs e)
        {
            if (lstDsHoaDon.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "Chưa chọn hóa đơn!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            order = (Order)lstDsHoaDon.SelectedItems[0].Tag;
            DisplayOrderDetail();
        }

        private void dtpNgayDau_ValueChanged_1(object sender, EventArgs e)
        {
            dtpNgayCuoi.MinDate = dtpNgayDau.Value;
        }

        private void dtpNgayCuoi_ValueChanged_1(object sender, EventArgs e)
        {
            dtpNgayDau.MaxDate = dtpNgayCuoi.Value;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                order.Update();
                MessageBox.Show(this, "Cập nhật trạng thái thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DisplayOrderDetail();
            }
            catch
            {
                return;
            }
        }
        public void loadCbxStatus()
        {
            Dictionary<string, string> test = new Dictionary<string, string>();
            test.Add("-1", "Tất cả");
            test.Add("1", "Chưa thanh toán");
            test.Add("0", "Đã thanh toán");
            cbxStatus.DataSource = new BindingSource(test, null);
            cbxStatus.DisplayMember = "Value";
            cbxStatus.ValueMember = "Key";
        }
        #endregion

        #region receipt
        public void loadDsPhieuNhap()
        {
            lstDsPhieuNhap.Items.Clear();
            foreach (var r in receipts)
            {
                ListViewItem lvi = new ListViewItem(r.Id.ToString());
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, String.Format("{0:d/M/yyyy}", r.Time));
                lvi.SubItems.Add(sit);
                lvi.Tag = r;
                lstDsPhieuNhap.Items.Add(lvi);

            }
        }

        public void DisplayReceiptDetail()
        {
            lstChiTietPhieuNhap.Items.Clear();
            int Total = 0;
            foreach (var o in receipt.Products)
            {
                int subTotal = o.Quantity * o.Price;
                ListViewItem lvi = new ListViewItem(o.Product.ToString());
                ListViewItem.ListViewSubItem sit = new ListViewItem.ListViewSubItem(lvi, o.Price.ToString());
                ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem(lvi, o.Quantity.ToString());
                ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem(lvi, subTotal.ToString());
                lvi.SubItems.Add(sit);
                lvi.SubItems.Add(sit1);
                lvi.SubItems.Add(sit2);
                lvi.Tag = o;
                lstChiTietPhieuNhap.Items.Add(lvi);
                Total += subTotal;
            }
            txtTotal2.Text = Total.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (lstDsPhieuNhap.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "Chưa chọn phiếu nhập!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            receipt = (ReceiptNote)lstDsPhieuNhap.SelectedItems[0].Tag;
            DisplayReceiptDetail();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DateTime from = DateTime.Parse(string.Format("{0} 00:00:00", String.Format("{0:MM/dd/yyyy}", dtpNgayDau2.Value)));
            DateTime to = DateTime.Parse(string.Format("{0} 23:59:59", String.Format("{0:MM/dd/yyyy}", dtpNgayCuoi2.Value)));
            receipt = new ReceiptNote();
            receipts = receipt.GetAllByDate(from, to);
            loadDsPhieuNhap();
        }

        private void chkStatus_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có chắc muốn thay đổi trạng thái của háo đơn?", "Cảnh báo", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                order.Status = OrderStatus.PAID;
            }
        }

        private void dtpNgayDau2_ValueChanged_1(object sender, EventArgs e)
        {
            dtpNgayCuoi.MinDate = dtpNgayDau.Value;
        }

        private void dtpNgayCuoi2_ValueChanged_1(object sender, EventArgs e)
        {
            dtpNgayDau.MaxDate = dtpNgayCuoi.Value;
        }
        #endregion
        

    }
}
